package com.sntk.organauthomanager.infrastructure.convert;

import com.sntk.organauthomanager.domain.model.RoleIdentify;
import com.sntk.organauthomanager.domain.model.RoleResource;
import com.sntk.organauthomanager.infrastructure.db.entity.RoleResourceEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName: RoleResourceConvert
 * @author: wgchao
 * @createTime: 2020/7/21 7:34 PM
 * @Description: TODO
 */
@Component
public class RoleResourceConvert {


    public List<RoleResourceEntity> convert(RoleResource roleResource){
        if(roleResource.getResources()==null || roleResource.getResources().isEmpty()){
            return null;
        }
        List<RoleResourceEntity> roleResourceEntities = new ArrayList<>();
        for(String mark : roleResource.getResources()){
            RoleResourceEntity roleResourceEntity = new RoleResourceEntity();
            roleResourceEntity.setInstitutionNumber(roleResource.getRoleIdentify().getInstitutionNumber());
            roleResourceEntity.setRoleCode(roleResource.getRoleIdentify().getRoleCode());
            roleResourceEntity.setResourceMark(mark);
            roleResourceEntities.add(roleResourceEntity);
        }
        return roleResourceEntities;
    }

    public RoleResource convert(List<RoleResourceEntity> roleResourceEntities){
        if(roleResourceEntities==null || roleResourceEntities.isEmpty()){
            return null;
        }
        Set<String> resources = new HashSet<>();
        for(RoleResourceEntity roleResourceEntity : roleResourceEntities){
            resources.add(roleResourceEntity.getResourceMark());
        }
        return new RoleResource(new RoleIdentify(roleResourceEntities.get(0).getInstitutionNumber(),
                roleResourceEntities.get(0).getRoleCode()), resources);
    }
}
